package view;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;

import entity.MonthlyCard;
import main.Main;
import util.Constants;
import util.DateTimeUtils;
import util.Validator;

public class AddMonthlyCardJPanel extends JPanel {
	private static final long serialVersionUID = 2543185054428597662L;

	public AddMonthlyCardJPanel(String parkId){
        setLayout(null);
        initUI(parkId);
	}
	
	private void initUI(String parkId){
		JLabel lab1 = new JLabel("车位号",SwingConstants.RIGHT);
		lab1.setFont(Constants.font);
		lab1.setBounds(100, 30, 60, 30);
	        
        JLabel lab1input = new JLabel(parkId,SwingConstants.LEFT);
        lab1input.setBounds(165, 30, 120, 30);

        JLabel lab2 = new JLabel("车牌号",SwingConstants.RIGHT);
        lab2.setFont(Constants.font);
        lab2.setBounds(100, 90, 60, 30);
		
        JTextField lab2input = new JTextField();
        Validator.bind(lab2input,Validator.CARID);
        lab2input.setBounds(165, 90, 120, 30);		
		
        JLabel lab3 = new JLabel("月数",SwingConstants.RIGHT);
        lab3.setFont(Constants.font);
        lab3.setBounds(40, 150, 120, 30);
        
        JTextField lab3input = new JTextField();
        Validator.bind(lab3input,Validator.NUM);
        lab3input.setBounds(165, 150, 120, 30);
        
        JLabel lab4 = new JLabel("开始日期",SwingConstants.RIGHT);
        lab4.setFont(Constants.font);
        lab4.setBounds(100, 210, 60, 30);
        
        JTextField lab4input = new JTextField();
        Validator.bind(lab4input,Validator.DATE);
        lab4input.setBounds(165, 210, 120, 30);
        
        
        JButton btn = new JButton("打印账单");
		btn.setBounds(165, 270, 120, 30);
		btn.setFont(Constants.font);
		btn.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				String lab2inputStr = lab2input.getText().trim();
				String lab3inputStr = lab3input.getText().trim();
				String lab4inputStr = lab4input.getText().trim();
				
				
				if (Validator.isEmpty(lab2inputStr)) {
					JOptionPane.showMessageDialog(null, "请输入车牌号！");
					return;
				}
				
				if (MonthlyCard.isExist(lab2inputStr)) {
					JOptionPane.showMessageDialog(null, "车牌号冲突，不可重复开卡！");
					return;
				}
				
				if (Validator.isEmpty(lab3inputStr)) {
					JOptionPane.showMessageDialog(null, "请输入开始日期！");
					return;
				}
				
				if (Validator.isEmpty(lab4inputStr)) {
					JOptionPane.showMessageDialog(null, "请输入月数！");
					return;
				}
								
				MonthlyCard card = new MonthlyCard();
				card.parkId = parkId;
				card.carId = lab2inputStr;
				card.month = Integer.valueOf(lab3inputStr);
				card.startDate = lab4inputStr;
				card.endDate = DateTimeUtils.dPlusMonths(card.startDate, card.month.longValue());
				card.createTime = DateTimeUtils.dtNow();
				
				JDialog dlg = new JDialog(Main.frame, "月卡账单",true);
				Main.dlgStack.push(dlg);
//				dlg.setBounds(300, 200, 450, 500);
				dlg.setSize(450, 500);
				dlg.setLocationRelativeTo(null);
				dlg.add(new AddMonthlyReceiptJPanel(card));
				dlg.validate();
		        dlg.addWindowListener(new WindowAdapter() {
		        	@Override
		            public void windowClosing(WindowEvent e) {
		        		if(Main.dlgStack.size() == 3){
		        			Main.dlgStack.pop().dispose();
		        		}
		            }
				});
		        dlg.setVisible(true);
			}
		});
		
		
		add(lab1);
		add(lab1input);
		add(lab2);
		add(lab2input);
		add(lab3);
		add(lab3input);
		add(lab4);
		add(lab4input);
		add(btn);
	}
}
